﻿#include "protreeitem.h"
#include "const.h"
#include <QDebug>

ProTreeItem::ProTreeItem(QTreeWidget *view, const QString &name,
                         const QString &path, int type)
    :QTreeWidgetItem(view,type),_path(path),_name(name),_root(this),_pre_item(nullptr),
    _next_item(nullptr)
{

}

ProTreeItem::ProTreeItem(QTreeWidgetItem *parent, const QString &name, const QString &path,
                         QTreeWidgetItem *root, int type)
    :QTreeWidgetItem(parent,type),_path(path),_name(name),_root(root),_pre_item(nullptr),
    _next_item(nullptr)
{

}

const QString &ProTreeItem::GetPath()
{
    return _path;
}

QTreeWidgetItem *ProTreeItem::GetRoot()
{
    return _root;
}

void ProTreeItem::SetPreItem(QTreeWidgetItem *item)
{
    _pre_item = item;
}

void ProTreeItem::SetNextItem(QTreeWidgetItem *item)
{
    _next_item = item;
}

ProTreeItem *ProTreeItem::GetPreItem()
{
    return dynamic_cast<ProTreeItem*>(_pre_item);
}

ProTreeItem *ProTreeItem::GetNextItem()
{
    return dynamic_cast<ProTreeItem*>(_next_item);
}


ProTreeItem *ProTreeItem::GetLastPicChild()
{
    qDebug() << u8"进入GetLastPicChild了\n";
    if(this->type() == TreeItemPic)
    {
        return nullptr;
    }

    auto child_count = this->childCount();
    qDebug() << "child_count = " << child_count << endl;
    if(child_count == 0)
    {
        return nullptr;
    }

    qDebug() << u8"开始遍历了\n";
    //遍历目录树
    for(int i = child_count-1; i>=0; i--)
    {
        auto * last_child = this->child(i);
        auto * last_tree_item = dynamic_cast<ProTreeItem*>(last_child);
        if(last_tree_item->type() == TreeItemPic)
        {
            return last_tree_item;
        }

        last_child = last_tree_item->GetLastPicChild();
        if(!last_child)
        {
            continue;
        }
        last_tree_item = dynamic_cast<ProTreeItem*>(last_child);
        return last_tree_item;
    }

    return nullptr;
}

ProTreeItem *ProTreeItem::GetFirstPicChild()
{
    if(this->type() == TreeItemPic)
    {
        return nullptr;
    }

    auto child_count = this->childCount();
    if(child_count == 0)
    {
        return nullptr;
    }


    //遍历目录树
    for(int i = 0; i<= child_count - 1; i++)
    {
        auto * first_child = this->child(i);
        auto * first_tree_item = dynamic_cast<ProTreeItem*>(first_child);
        if(first_tree_item->type() == TreeItemPic)
        {
            return first_tree_item;
        }

        first_child = first_tree_item->GetFirstPicChild();
        if(!first_child)
        {
            continue;
        }
        first_tree_item = dynamic_cast<ProTreeItem*>(first_child);
        return first_tree_item;
    }

    return nullptr;
}

